//
//  MZYhotTableViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYhotTableViewController.h"
#import "MZYHeader.h"
#import "MZYhotTableViewCell.h"
#import "MZYhotWebTabViewController.h"
@interface MZYhotTableViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *hotTableView;
@property(nonatomic, retain)NSMutableArray *hotArr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, assign)NSInteger page;

@property(nonatomic, retain)NSMutableArray *huancunArr;

@end

@implementation MZYhotTableViewController

- (void)dealloc {
    [_hotTableView release];
    [_hotArr release];
    [_HUD release];
    [_huancunArr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
      self.hotArr = [NSMutableArray array];
    [self createView];
    [self createData];
}

- (void)createView {
    // 建立ScrollView上第二个tableView
    self.hotTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.hotTableView.rowHeight = 110;
    [self.view addSubview:self.hotTableView];
    [self.hotTableView release];
    
    self.hotTableView.dataSource = self;
    self.hotTableView.delegate = self;
    
    //下拉
    self.hotTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
    //上啦
    self.hotTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    
    self.page = 1;
    
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;
    

}

//下拉
- (void)header {
    //把已经刷新的数据删除,否则越刷新重复数据越多
    [self createData];
    
}
//上拉
-(void)footer {
    self.page ++;
    NSLog(@"%ld",(long)self.page);
    NSMutableArray *tempArr = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://www.xbiao.com/app/articlelist?&page=%ld&type=2", (long)self.page];
    AFHTTPRequestOperationManager *hotmanager = [AFHTTPRequestOperationManager manager];
    [hotmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *lArr = responseObject;
      
        for (NSDictionary *tempDic in lArr) {
            MZYInformationModel *info = [[MZYInformationModel alloc] init];
            [info setValuesForKeysWithDictionary:tempDic];
          
            [self.hotArr addObject:info];
            [info release];
        }
        if (self.hotArr.count > 0) {
//            self.HUD.hidden = YES;
            [self.hotTableView reloadData];
        }
        [self.hotTableView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}


- (void)createData {
    // 解析HotTableView
//    AFHTTPRequestOperationManager *hotmanager = [AFHTTPRequestOperationManager manager];
//    [hotmanager GET:@"http://www.xbiao.com/app/articlelist?&page=0&type=2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableArray *lArr = responseObject;
//        self.hotArr = [NSMutableArray array];
//        for (NSDictionary *tempDic in lArr) {
//            MZYInformationModel *info = [[MZYInformationModel alloc] init];
//            [info setValuesForKeysWithDictionary:tempDic];
//            [self.hotArr addObject:info];
//            [info release];
//        }
//        if (self.hotArr.count > 0) {
//            self.HUD.hidden = YES;
//            [self.hotTableView reloadData];
//        }
//        [self.hotTableView.header endRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
    
    NSURL *url = [NSURL URLWithString:@"http://www.xbiao.com/app/articlelist?&page=0&type=2"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
        
            self.huancunArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            
            
            NSMutableArray *lArr = self.huancunArr;
            self.hotArr = [NSMutableArray array];
            
            for (NSDictionary *tempDic in lArr) {
                MZYInformationModel *info = [[MZYInformationModel alloc] init];
                [info setValuesForKeysWithDictionary:tempDic];
                [self.hotArr addObject:info];
                
                [info release];
            }
            
            if (self.hotArr.count > 0) {
                self.HUD.hidden = YES;
                [self.hotTableView reloadData];
            }
            [self.hotTableView.header endRefreshing];
        }
        
    }];
    
    NSURLCache *cache = [NSURLCache sharedURLCache];
    NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
    if (response) {
        NSLog(@"源这个请求已存在缓存");
    } else {
        NSLog(@"源这个请求没有缓存");
    }
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hotArr.count;
}
// tableView协议必须实现的第二个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MZYInformationModel *info = [[MZYInformationModel alloc] init];
    static NSString *reusea = @"reusea";
    MZYhotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusea];
    if (!cell) {
        cell = [[[MZYhotTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reusea] autorelease];
    }
    info = self.hotArr[indexPath.row];
    cell.titleLabel.text = info.title;
    NSString *str = [info.inputtime substringToIndex:11];
    cell.timeLabel.text = str;
    cell.readLabel.text = [NSString stringWithFormat:@"%@%@",@"阅读量:", info.hits] ;
    [cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:info.img] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点击了😠");
        MZYhotWebTabViewController *hotabVC = [[MZYhotWebTabViewController alloc] init];
        hotabVC.hidesBottomBarWhenPushed = YES;
        MZYInformationModel *info = self.hotArr[indexPath.row];
        hotabVC.url = info.url;
        [self.navigationController pushViewController:hotabVC animated:YES];
    [hotabVC release];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
